package com.zzy.demo.service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.persistence.EntityManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzy.cms.core.base.service.BaseServiceImpl;
import com.zzy.demo.dao.FileLiteDao;
import com.zzy.demo.model.FileLite;

@Service
public class FileLiteService extends BaseServiceImpl<FileLiteDao, FileLite, String> {

	@Autowired
	FileLiteDao fileLiteDao;

	public void saveFile() throws IOException {
		// 假设你已经有了一个EntityManager实例
		EntityManager em = fileLiteDao.getEntityManager();

		// 创建文件实体
		FileLite fileEntity = new FileLite();
		fileEntity.setFileName("example.txt");
		fileEntity.setFileType("text/plain");

		// 读取文件到字节数组
		byte[] fileContent = Files.readAllBytes(Paths.get("path/to/your/file.txt"));
		fileEntity.setData(fileContent);

		// 开启事务
		em.getTransaction().begin();

		// 保存实体到数据库
		em.persist(fileEntity);

		// 提交事务
		em.getTransaction().commit();

		// 关闭实体管理器
		em.close();
	}

}
